package com.morningstar.qs.api.basic.common.encrypt.rsa;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.SecureRandom;


public class GenKey {
	private static final String RSA = "RSA";
	public static final String PKEY_NAME="pk.dat";
	public static final String SKEY_NAME="sk.dat";
	public static final String Seed_KEY = "Morningstar.Markets";
	public static void genKeyToFile(String path){
		try{
			KeyPairGenerator keygen = KeyPairGenerator.getInstance(RSA);
			SecureRandom random = new SecureRandom();
			random.setSeed(Seed_KEY.getBytes());
			keygen.initialize(512, random);
			KeyPair kp = keygen.generateKeyPair();
			PublicKey publicKey = kp.getPublic();
			saveToFile(publicKey, path, PKEY_NAME);
			PrivateKey privateKey = kp.getPrivate();
			saveToFile(privateKey, path, SKEY_NAME);
		}catch(NoSuchAlgorithmException e){
			e.printStackTrace();
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	public static Object genKey(String path,Boolean isPrivate){
		String fileName=isPrivate?SKEY_NAME:PKEY_NAME;
		if(!existSecDesFile(path,fileName)){
			genKeyToFile(path);
		}
		return readFromFile(path,fileName);
	}
	private static Boolean existSecDesFile(String path,String fileName){
		String temp=null;
		File newfile=null;
		if(path.endsWith("/")||path.endsWith("\\")){
			temp=path;
		}else{
			temp=path+"/";
		}
		
		File pathFile = new File(temp);
		if(!pathFile.exists()){
			return false;
		}
		newfile = new File(temp+fileName);
		return newfile.exists();
	}
	private static void saveToFile(Object obj,String path,String fileName){
		try{
			ObjectOutputStream output = new ObjectOutputStream(new FileOutputStream(genFile(path,fileName)));
			output.writeObject(obj);
			output.close();
		}catch(FileNotFoundException e){
			e.printStackTrace();
		}catch(IOException e){
			e.printStackTrace();
		}
	}
	private static Object readFromFile(String path,String fileName){
		Object obj = null;
		try{
			ObjectInputStream input = new ObjectInputStream(new FileInputStream(
					genFile(path,fileName)));
			obj = input.readObject();
			input.close();
		}catch(FileNotFoundException e){
			e.printStackTrace();
		}catch(IOException e){
			e.printStackTrace();
		}catch(Exception e){
			e.printStackTrace();
		}
		return obj;
	}
	private static File genFile(String path,String fileName){
		String temp=null;
		File newfile=null;
		if(path.endsWith("/")||path.endsWith("\\")){
			temp=path;
		}else{
			temp=path+"/";
		}
		
		File pathFile = new File(temp);
		if(!pathFile.exists()){
			pathFile.mkdirs();
		}
		newfile = new File(temp+fileName);
		return newfile;
	}
}
